//
//  ProvViewController.m
//  EPGChina
//
//  Created by CreeveLiu on 15/1/15.
//  Copyright (c) 2015年 sumavision. All rights reserved.
//

#import "ProvViewController.h"
#import "NetworkUtil.h"
#import "ChannelModel.h"
#import "EPGViewController.h"
#import "BookedViewController.h"

@interface ProvViewController ()

@end

@implementation ProvViewController
{
    NSMutableArray      *_arrProvChannels;
    NSMutableArray      *_arrPlayingEPGs;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self reloadTableViewContent];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setupUI
{
    self.navigationItem.title = NSLocalizedString(@"卫视频道", nil);
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@""
                                                                             style:UIBarButtonItemStylePlain
                                                                            target:nil
                                                                            action:nil];
    self.refreshControl = [[UIRefreshControl alloc] init];
    [self.refreshControl addTarget:self
                            action:@selector(reloadTableViewContent)
                  forControlEvents:UIControlEventValueChanged];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(shouldGotoEpgViewController:)
                                                 name:NOTI_SHOULD_GOTO_EPGVIEW object:nil];
    
}

- (void)reloadTableViewContent
{
    if (_arrProvChannels == nil) {
        _arrProvChannels = [[NSMutableArray alloc] initWithCapacity:0];
    } else {
        [_arrProvChannels removeAllObjects];
    }
    
    if (_arrPlayingEPGs == nil) {
        _arrPlayingEPGs = [[NSMutableArray alloc] initWithCapacity:0];
    } else {
        [_arrPlayingEPGs removeAllObjects];
    }
    
    [NetworkUtil getProvChannelWithCompleted:^(NSArray *array, NSError *error) {
        if (error == nil) {
            [_arrProvChannels addObjectsFromArray:array];
            [self.tableView reloadData];
        } else {
            [SVProgressHUD showErrorWithStatus:error.localizedDescription];
        }
        [self.refreshControl endRefreshing];
    }];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _arrProvChannels.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
    }
    if (_arrProvChannels.count > indexPath.row) {
        ChannelModel *channel = [_arrProvChannels objectAtIndex:indexPath.row];
        [cell.textLabel setText:channel.title];
        [cell.detailTextLabel setText:@""];
    }
    return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


#pragma mark - Navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    ChannelModel *selectedChannel = [_arrProvChannels objectAtIndex:indexPath.row];
    EPGViewController *epgVC = [segue destinationViewController];
    epgVC.channel = selectedChannel;
}

- (void)shouldGotoEpgViewController:(NSNotification *)noti
{
    if (self.isVisible) {
        ChannelModel *channel = [[ChannelModel alloc] init];
        channel.channelId = [noti.userInfo objectForKey:@"channelId"];
        channel.title = [noti.userInfo objectForKey:@"channelName"];
        EPGViewController *epgVC = [[EPGViewController alloc] init];
        epgVC.channel = channel;
        [self.navigationController pushViewController:epgVC animated:YES];
    }
}


- (IBAction)didClickedBookListButton:(UIBarButtonItem *)sender
{
    BookedViewController *bookedVC = [[BookedViewController alloc] init];
    UINavigationController *bookedNC = [[UINavigationController alloc] initWithRootViewController:bookedVC];
    [self presentViewController:bookedNC animated:YES completion:nil];
}
@end
